﻿/*

知识结构模版页面

*/
//
angular.module('knowledgeStructureModelAddEdit', ['Common', 'loadModule', 'knowledgebaseS', '720kb.tooltips'])
 .directive("knowledgeStructureModelAddEdit", ["$rootScope", "$stateParams", "$timeout", "sysUserInfo", "RequestTrainService", "Msg", "Comm", function ($rootScope, $stateParams, $timeout, sysUserInfo, RTS, Msg, Comm) {
     return {
         restrice: "AE",
         scope: {
             isSave: "=", // 需不需要保存数据
             isSaveBtn: "=",
             modelSaveBtn: "=", // 是否正在请求中
             modelIsHide: "=",    //是否显示出来这个内容 
             modelItemS: "=", //知识结构模版集合                          
             modelOnSave: "&",      //保存对象到服务器         (目前修改的只有这个字段有用)            
             modelOnHide: "&"
         },
         templateUrl: "/system/directive/train/knowledgestructure/knowledge_structure_model_add_edit.html",
         link: function (scope, element, attr) {
             scope.isEdit = false;  // 是否进行编辑
             scope.modelIsHide = false;

             scope.hadModelIds = ""; //已今拥有的模板id
             // 全局loading
             scope.modelLoading = false;
             //关闭窗口
             scope.isModelHideFun = function () {
                 scope.modelOnHide(); // 对方父节点只需要调用关闭模版就行了
                 scope.modelIsHide = false;
                 scope.isSaveBtn = false;
             }
             // 初始化
             scope.paramodel = { "model_Id": "A-1001" }; //模板参数初始化             
             scope.modelList = []; //模板列表
             //获取模版列表
             scope.getModellist = function (paramm) {
                 scope.modelLoading = true;
                 RTS.findModelList(paramm).then(function (response) {
                     scope.modelLoading = false;   //关闭loading
                     if (response.errorcode == "0") {// 成功       
                         scope.modelList = response.datas;
                         // 选中样式                     
                         scope.parentStyle(paramm.model_Id);
                         scope.flagCnode();
                     } else {   // 获取失败 
                         console.log("获取失败！");
                     }
                 }, function errorCallback() {
                     scope.modelLoading = false;   //关闭loading
                     console.log("连接服务器失败！");
                 });
             }
             scope.flagCnode = function () {
                 // 判断需不需要保存企业的知识分类
                 if (!scope.isSave) {
                     scope.modelLoading = true;
                     //查询选中的子节点
                     scope.params = { "orgId": sysUserInfo.getuserinfo().organization_ID, "powerLV": sysUserInfo.getuserinfo().powerLV };
                     RTS.findhasId(scope.params).then(function (response) {
                         scope.modelLoading = false;
                         //response = JSON.parse(response);
                         if (response.errorcode == "0") {// 成功                               
                             var oldids = response.errormsg.split(',');
                             if (!Comm.isNull(oldids)) {
                                 scope.hadModelIds = oldids;
                                 angular.forEach(oldids, function (data, index, array) {
                                     if (!Comm.isNull(data)) {
                                         // console.log(data);
                                         scope.isEdit = true;
                                         scope.parentSel(data);
                                         //    angular.forEach(scope.modelList, function (datac, indexc, arrayc) {                                 
                                         /// });
                                     }
                                 });
                             }
                         } else {   // 获取失败 
                             console.log("获取失败！");
                         }
                     }, function errorCallback() {
                         scope.modelLoading = false;   //关闭loading
                         console.log("连接服务器失败！");
                     });
                 } else {
                     // 标记选中
                     angular.forEach(scope.modelItemS, function (data, index) {
                         angular.forEach(scope.modelList, function (fdata, index) {
                             if (data.modelId == fdata.model_Id) {
                                 fdata.isSel = true;
                             }
                         });
                     })
                 }
             }
             //根据父节点查询  子模板
             scope.findmodelbyfid = function (modelitem) {
                 if (scope.paramodel.model_Id == modelitem.model_Id) { //判断是不是第一次进来
                     scope.checkOnly(modelitem);
                 } else {
                     scope.paramodel.model_Id = modelitem.model_Id;

                     scope.getModellist(scope.paramodel);
                 }
             }
             // 匹配父节点选中样式
             scope.parentStyle = function (id) {
                 angular.forEach(scope.modelList, function (data, index, array) {
                     if (data.model_Id == id) {
                         data.isStyle = true;
                     }
                 });
             }
             // 匹配编辑之前的选中的样式
             scope.parentSel = function (id) {
                 angular.forEach(scope.modelList, function (data, index, array) {
                     if (data.model_Id == id) {
                         data.isSel = true;
                     }
                 });
             }
             // 单选
             scope.checkOnly = function (item) {
                 if (item.isSel) {  //不选中
                     item.isSel = false;
                     scope.recursionNode(item.model_Id, item.isSel); // 不选子节点
                 } else {
                     item.isSel = true;
                     scope.recursionParent(item.parentId);  // 选中父节点
                     scope.recursionNode(item.model_Id, item.isSel); // 选中子节点
                 }
                 // 设置参数
                 scope.setParams();
             }
             //递归父节点   （只有选中的时候和父节点有关系）有死循环的可能
             scope.recursionParent = function (id) {
                 angular.forEach(scope.modelList, function (data, index, array) {
                     if (data.model_Id == id) {
                         data.isSel = true;
                         scope.recursionParent(data.parentId);
                     }
                 });
             }
             // 递归子节点   （选不选中都有关系） 有死循环的可能 
             scope.recursionNode = function (id, isSel) {
                 angular.forEach(scope.modelList, function (data, index, array) {
                     if (data.parentId == id) {
                         data.isSel = isSel;
                         scope.recursionNode(data.model_Id, isSel);
                     }
                 });
             }
             // 设置参数 （可能慢）  本来放在请求方法内的，放入之后控制器上面接收不到值。所以拆出
             scope.setParams = function () {
                 if (!scope.modelItemS) {
                     scope.modelItemS = [];
                 }
                 scope.modelItemS.length = 0;
                 //设置参数                                  
                 angular.forEach(scope.modelList, function (data, index, arr) {
                     if (data.isSel)
                         scope.modelItemS.push({ "modelLV": data.model_Level, "modelId": data.model_Id, "modelName": data.model_Name, "parentId": data.parentId });

                 });
             }
             // 初始化
             scope.getModellist(scope.paramodel);
             //保存知识结构
             scope.saveModel = function () {
                 console.log(scope.modelItemS);
                 item = {
                     powerLV: sysUserInfo.getuserinfo().powerLV,
                     userId: sysUserInfo.getuserinfo().user_ID,
                     userName: sysUserInfo.getuserinfo().username,
                     OrgId: sysUserInfo.getuserinfo().organization_ID,   // 企业id
                     OrgName: sysUserInfo.getuserinfo().organization_Name,
                     modelObj: scope.modelItemS,
                     delmodelObj: [],
                     isEdit: scope.isEdit//添加-fasle，编辑-true
                 };
                 // 判断是不是保存知识分类 
                 if (!scope.isSave) {
                     angular.forEach(scope.hadModelIds, function (data1, index1, arr1) {
                         angular.forEach(item.modelObj, function (data2, index2, arr2) {
                             if (data1==data2.modelId) {
                                 item.modelObj.splice(index2, 1);
                             }
                         });
                     });
                     
                     scope.modelLoading = true; // 更换为圆圈的loading                 
                     RTS.addModelKnowledge(item).then(function (response) {
                         scope.modelLoading = false;  //关闭loading
                         if (response.errorcode == "0") {// 成功       
                             Msg.success("保存成功！");
                             scope.modelOnSave();
                         } else {   // 获取失败 
                             Msg.error("获取失败！");
                         }
                     }, function errorCallback() {
                         scope.modelLoading = false;  //关闭loading
                         //            console.log("连接服务器失败！");
                         Msg.error("连接服务器失败！");
                     });
                 } else {
                     scope.modelOnSave();
                 }

                 //scope.modelOnSave();
             }
         }
     }
 } ]);

